package me.zhengjie.timing.plc;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.entity.WcsLineBodyInbound;
import me.zhengjie.pojo.entity.WcsLineBodyOutbound;
import me.zhengjie.pojo.entity.WcsRetryPlc;
import me.zhengjie.sqlservice.WcsLineBodyInboundService;
import me.zhengjie.sqlservice.WcsLineBodyOutboundService;
import me.zhengjie.sqlservice.WcsRetryPlcService;
import me.zhengjie.tcp.FluentFrame;
import me.zhengjie.utility.state.MaterialStatus;
import me.zhengjie.utility.state.TaskType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
@RequiredArgsConstructor
public class WcsRetryPlcTiming {
//    private final WcsLineBodyInboundService wcsLineBodyInboundService;
//
//    private final InboundLineBody inboundLineBody;
//
//    private final WcsLineBodyOutboundService wcsLineBodyOutboundService;
//
//    private final OutboundLineBody outboundLineBody;

    private final FluentFrame fluentFrame;

    private final WcsRetryPlcService wcsRetryPlcService;

    @Scheduled(fixedDelay = 1000)
    public void WcsRetryPlc(){
        try {
            List<WcsRetryPlc> wcsRetryPlcList = wcsRetryPlcService.selectFloor(TaskType.TWO_FLOOR.getMessage());
            if (!wcsRetryPlcList.isEmpty()){
                for (WcsRetryPlc wcsRetryPlc : wcsRetryPlcList){
                    Integer address = wcsRetryPlc.getAddress();
                    Integer value = wcsRetryPlc.getValue();
                    String region = wcsRetryPlc.getRegion ();
                    Integer id = wcsRetryPlc.getId();
                    boolean b1 = fluentFrame.writeWithRetry(address, value,1, MaterialStatus.RETRY_COUNT.getStatus(), region);
                    if (b1){
                        //删除
                        wcsRetryPlcService.deleteId(id);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void retryPlc(boolean inboundLineBody, Integer id) {
        boolean b = inboundLineBody;
        if (b) {
            //删除
            wcsRetryPlcService.deleteId(id);
        }
    }
}
